package com.njts.servlet;

import com.njts.auto.AutoCreateBean;
import com.njts.auto.AutoSearch;
import com.njts.bean.Score;
import com.njts.bean.Subject;
import com.njts.dao.UseDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

@WebServlet("/scoreAdd")
public class ScoreAddServlet extends HttpServlet {
    UseDao useDao=new UseDao();
    AutoCreateBean autoCreateBean=new AutoCreateBean();
    AutoSearch autoSearch=new AutoSearch();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        Score score=new Score();
        String valuesComplementSql;
        try {
            score =(Score) autoCreateBean.createBean(req, score);
            valuesComplementSql = autoSearch.getValuesComplementSql(score);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        useDao.insertScore(valuesComplementSql);
        //
        ArrayList<Score> scoreList=(ArrayList<Score>) useDao.selectScore();
        ArrayList<Subject> subjectList=(ArrayList<Subject>) useDao.selectSubject();
        req.setAttribute("subjectList",subjectList);
        req.setAttribute("scoreList",scoreList);
        req.setAttribute("scoreCount",20); //先写死后面改
        req.getRequestDispatcher("/view/score.jsp").forward(req,resp);
    }
}
